/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import Common.NetworkManager;
import Constants.*;
import Main.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author Hmong
 */
public class MainForm extends Form implements CommandListener {
    private static MainForm _Instance = null;

    private TextField _DomainTextField = null;

    private Command _ExitCommand = null; // The exit command
    private Command _QueryCommand = null; // The login command
    private Command _TestNetworkCommand = null;

    public MainForm()
    {
        super(Constant.MainForm_Title);
        _Instance = this;

        NetworkManager.TestConnectNetwork();

        _DomainTextField = new TextField("域名：", "hmong.cn", 40, TextField.ANY);

        //String testEds = EncodingUtility.GetSupportedEncodings();
        //String eds = System.getProperty("microedition.encoding");

        //StringItem si = new StringItem("", "");
        //si.setLabel(eds);
        //si.setText(testEds);
        
        /*String strMsg = "CMWap: " + String.valueOf(bCMWap) + "\r\n";
        strMsg += "CMNet: " + String.valueOf(bCMNet);
        si.setText(strMsg);
        append(si);*/

        append(_DomainTextField);
    }

    public static MainForm GetInstance()
    {
        return _Instance;
    }

    public void ConnectTypeTested(int nType) {
        /*String strMsg = "";
        if (nType == ConnectType.CMNet.Http)
            strMsg += "CMNET Http " + ((0 != (NetworkManager.ConnectType & ConnectType.CMNet.Http)) ? "OK" : "Fail");
        else if (nType == ConnectType.CMNet.Socket)
            strMsg += "CMNET Socket " + ((0 != (NetworkManager.ConnectType & ConnectType.CMNet.Socket)) ? "OK" : "Fail");
        else if (nType == ConnectType.CMWap.Http)
            strMsg += "CMWAP Http " + ((0 != (NetworkManager.ConnectType & ConnectType.CMWap.Http)) ? "OK" : "Fail");
        else
            return;

        StringItem si = new StringItem("", strMsg);
        append(si);

        if ((!_CommandUpdated) && (0 != NetworkManager.ConnectType)){
            _CommandUpdated = true;

            addCommand(_QueryCommand);
            addCommand(_ExitCommand);

            setCommandListener(this);
        }*/
    }

    public void ConnectTypeTestCompleted() {
        if (0 == NetworkManager.ConnectSuccessfulType){
            if (null == _TestNetworkCommand) {
                _TestNetworkCommand = new Command("测试网络", Command.OK, 0);
                addCommand(_TestNetworkCommand);
                setCommandListener(this);
            }
        }
        else {
            if (null != _TestNetworkCommand) {
                removeCommand(_TestNetworkCommand);
                _TestNetworkCommand = null;
            }

            _ExitCommand = new Command("退出", Command.EXIT, 0);
            _QueryCommand = new Command("查询", Command.OK, 0);

            addCommand(_QueryCommand);
            addCommand(_ExitCommand);

            setCommandListener(this);
        }
    }

    public void commandAction(Command cmd, Displayable s) {
        if (cmd == _ExitCommand) {
            Program.GetInstance().destroyApp(false);
            Program.GetInstance().notifyDestroyed();
        }

        if (cmd == _QueryCommand){
            String queryDomain = this._DomainTextField.getString();
            QueryingForm queryForm = new QueryingForm(queryDomain);
            Program.GetDisplay().setCurrent(queryForm);
        }
    }
}
